//
//  CreateProjectViewController.m
//  BigToBig
//
//  Created by mao on 16/7/14.
//  Copyright © 2016年 cn.cloudtp. All rights reserved.
//

#import "CreateProjectViewController.h"
#import "DownListViewController.h"
#import "EditViewController.h"
#import "CityViewController.h"
#import "PersonalInfomationViewController.h"
#import "FieldViewController.h"

@interface CreateProjectViewController ()<EditContentTableViewCellDelegate,UITextFieldDelegate>
{
    UILabel *userLabel;
    UILabel *contentLabel;

}

@property(nonatomic,strong)NSArray *proArr;
@property(nonatomic,strong)NSArray *phoneArr;
@property(nonatomic,strong)NSArray *proDetailArr;
@property(nonatomic,strong)NSArray *sectionArr;
@property(nonatomic,strong)NSArray *planArr;
@property(nonatomic,strong)NSMutableDictionary *dataDic;
@property (nonatomic , strong)NSMutableDictionary *resultDic;
@property(nonatomic,strong)NSString *text;

@property(nonatomic, copy)NSString *projectName;
@property(nonatomic, copy)NSString *proSynopsis;
@property(nonatomic, copy)NSString *proIntroduce;


@end

@implementation CreateProjectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.view.backgroundColor = [UIColor whiteColor];
    self.resultDic = [NSMutableDictionary dictionary];
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    titleLab.text = @"项目创建";
    titleLab.textColor = [UIColor whiteColor];
    [self setBackBar];
    titleLab.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLab;
    
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationController.navigationBar.barTintColor = COLOR_RGBA(77, 155, 255, 1);
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.tableView = tableView;
    self.tableView.rowHeight = 70;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self initDataSource];
    
}
#pragma mark 导航栏左右Button
- (void)setBackBar{
    
    BackButton *backBtn=[BackButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 44, 44);
    [backBtn addTarget:self action:@selector(backController:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backBtn.imageView setContentMode:UIViewContentModeLeft];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    [self.navigationItem setHidesBackButton:YES animated:YES];    
    
    BackButton *saveBtn=[BackButton buttonWithType:UIButtonTypeCustom];
    saveBtn.frame = CGRectMake(kScreenWidth - 44,0 , 44, 44);
    [saveBtn addTarget:self action:@selector(SaveMessage) forControlEvents:UIControlEventTouchUpInside];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [saveBtn setTitle:@"提交" forState:UIControlStateNormal];
    [saveBtn.imageView setContentMode:UIViewContentModeLeft];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:saveBtn];
    [self.navigationItem setHidesBackButton:YES animated:YES];
    
}
#pragma mark 返回
- (void)backController:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - EditContentTableViewCellDelegate 代理方法实现
- (void)saveMessageWithValue:(NSString *)value andKey:(NSString *)key{
    
    if ([key isEqualToString:@"项目名称"]) {
        key = @"proName";
    }else if([key isEqualToString:@"项目梗概"]){
        key = @"prosynopsis";
    }
    else if ([key isEqualToString:@"项目介绍"]){
        key = @"proIntruduce";
    }
    [self.resultDic setObject:value forKey:key];
    
}

#pragma mark 提交
- (void)SaveMessage{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"继续完善内容" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self.navigationController dismissViewControllerAnimated:alert completion:nil];
    }];
    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {        
        
        NSUserDefaults* userID = [NSUserDefaults standardUserDefaults];
        NSString *UID = [userID objectForKey:@"userID"];
        
        NSString *url = [NSString stringWithFormat:@"http://61.135.176.43:6080/bigtobig/project/apply?user_id=%@&project_name=%@&project_logo=/project/asd&product_introduction=%@&areas=1|2&business_plan=/plan/zxc&applicant_postion=1&brief_introduction=%@&project_progress=2",UID,self.resultDic[@"proName"],self.resultDic[@"prosynopsis"],self.resultDic[@"proIntruduce"]];
        NSLog(@"%@",url);
        
//        [HttpTool postWithURL:url params:nil success:^(id json) {
//            
//        } failure:^(NSError *error) {
//            
//        }];
        
        ProjectInfoViewController *pro = [ProjectInfoViewController alloc];
        [self.navigationController pushViewController:pro animated:YES];
        [self pushContent];
    }];
    
    [alert addAction:cancel];
    [alert addAction:ok];
    [self.navigationController
     presentViewController:alert animated:YES completion:nil];
    
}

- (void)pushContent{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return _sectionArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return _phoneArr.count;
    }if (section == 1) {
        return _proArr.count;
    }if (section == 2) {
        return _proDetailArr.count;
    }if (section == 3){
        return _planArr.count;
    }
    return 0;
 
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0 || section == 1|| section == 2) {
        return 15;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (section == 0 || section == 1) {
        return 0.001f;
    }
    return 60;
}

- (void)initDataSource{
    
    self.sectionArr = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4", nil];
    
    NSDictionary *dic00 = [[NSDictionary alloc] initWithObjectsAndKeys:@"手机号",@"name",@"请输入手机号码",@"content", nil];
    self.phoneArr = [[NSArray alloc] initWithObjects:dic00, nil];
    
    NSDictionary *dic10 = [[NSDictionary alloc] initWithObjectsAndKeys:@"项目名称",@"name",@"简介清晰,传达意义明确",@"content", nil];
    
     NSDictionary *dic11 = [[NSDictionary alloc] initWithObjectsAndKeys:@"项目梗概",@"name",@"一句话介绍项目",@"content", nil];
     NSDictionary *dic12 = [[NSDictionary alloc] initWithObjectsAndKeys:@"项目介绍",@"name",@"介绍",@"content", nil];
    self.proArr = [[NSArray alloc] initWithObjects:dic10,dic11,dic12, nil];
    
     NSDictionary *dic20 = [[NSDictionary alloc] initWithObjectsAndKeys:@"所属领域",@"name",@"最多可选5个",@"content", nil];
    NSDictionary *dic21 = [[NSDictionary alloc] initWithObjectsAndKeys:@"所在城市",@"name",@"北京",@"content", nil];
    NSDictionary *dic22 = [[NSDictionary alloc] initWithObjectsAndKeys:@"所属阶段",@"name",@"请选择阶段",@"content", nil];
    NSDictionary *dic23 = [[NSDictionary alloc] initWithObjectsAndKeys:@"本人信息",@"name",@"请选择职位",@"content", nil];
    NSDictionary *dic24 = [[NSDictionary alloc] initWithObjectsAndKeys:@"项目标志",@"name",@"请点击上传标志",@"content", nil];
    self.proDetailArr = [[NSArray alloc] initWithObjects:dic20,dic21,dic22,dic23,dic24, nil];
    
    NSDictionary *dic30 = [[NSDictionary alloc] initWithObjectsAndKeys:@"商业计划书",@"name",@"点击上传标志",@"content", nil];
    self.planArr = [[NSArray alloc] initWithObjects:dic30, nil];
    
    _dataDic = [[NSMutableDictionary alloc]initWithObjectsAndKeys:_phoneArr,[_sectionArr objectAtIndex:0],_proArr,[_sectionArr objectAtIndex:1],_proDetailArr,[_sectionArr objectAtIndex:2],_planArr,[_sectionArr objectAtIndex:3],nil];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *str = [_sectionArr objectAtIndex:indexPath.section];
    NSArray *arr = [_dataDic objectForKey:str];
 
    static NSString *CellIdentifier = @"cell";
    EditContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[EditContentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.editContentCellDelegate = self;
    
    if (indexPath.section == 0) {

        NSUserDefaults* phoneNumberDefaults = [NSUserDefaults standardUserDefaults];
        NSString *number = [phoneNumberDefaults objectForKey:@"phoneNumber"];
        NSString *phoneNumber1 = [NSString stringWithFormat:@"%@",number];
        cell.leftLab.text = @"手机号";
        cell.contetField.text = phoneNumber1;
        cell.contetField.enabled = NO;

    }else if (indexPath.section == 1){
        cell.leftLab.text= [[arr objectAtIndex:indexPath.row] valueForKey:@"name"];
        cell.contetField.placeholder = [[arr objectAtIndex:indexPath.row] valueForKey:@"content"];
           
        [cell.contetField addTarget:self action:@selector(textFiledDidChange:) forControlEvents:(UIControlEventEditingChanged)];
        
    }else{

        NSString *name = [[arr objectAtIndex:indexPath.row] valueForKey:@"name"];
        cell.leftLab.text=name;
        cell.contetField.placeholder = [[arr objectAtIndex:indexPath.row] valueForKey:@"content"];
        cell.contetField.enabled = NO;
       
    }
     return cell;
    
}

- (void)textFiledDidChange:(id)sender{
    UITextField *_field = (UITextField *)sender;
    
    NSLog(@"%@",[_field text]);
}


#pragma mark 让分区标题随tableView滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat sectionHeaderHeight = 20;
    
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        
    }
    
    CGFloat sectionFootHeight = 60;
    
    if (scrollView.contentOffset.y >= sectionFootHeight && scrollView.contentOffset.y < 60) {
        scrollView.contentInset = UIEdgeInsetsMake(0, 0, -60, 0);
    }else if (scrollView.contentOffset.y == -60) {
        
        scrollView.contentInset = UIEdgeInsetsMake(60, 0, 0, 0);

    }
    else if (scrollView.contentOffset.y < 0 &&scrollView.contentOffset.y != -64){
        scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    else if (scrollView.contentOffset.y >= 23 && scrollView.contentOffset.y <= 83){
        scrollView.contentInset = UIEdgeInsetsMake(0, 0, -(83-scrollView.contentOffset.y), 0);
        
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor colorWithWhite:0.949 alpha:1.000];
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.numberOfLines = 0;
    titleLabel.font = [UIFont systemFontOfSize:14];
    [view addSubview:titleLabel];
    if (section == 2) {
        view.frame = CGRectMake(0, 0, kScreenWidth, 50);
        titleLabel.frame = CGRectMake(10, 0, kScreenWidth - 20, 60);
        titleLabel.text = @"项目标志非企业标志 像素在512x512x以上的正方形标志.支持JPG,GIF,PNG格式,文件不大于1MB";
    }if (section == 3) {
        view.frame = CGRectMake(0, 0, kScreenWidth, 50);
        titleLabel.frame = CGRectMake(10, 0, kScreenWidth - 20, 60);
        titleLabel.text = @"可上传PDF,DOC,DOCX,PPT,PPTX;文件大小不要超过20MB";
    }
    
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        EditViewController *editVC = [[EditViewController alloc] init];
        [self.navigationController pushViewController:editVC animated:YES];
        
        NSString *str = [_sectionArr objectAtIndex:indexPath.section];
        NSArray *arr = [_dataDic objectForKey:str];
        editVC.titleName = [[arr objectAtIndex:indexPath.row] valueForKey:@"name"];
        editVC.promptInfo = [[arr objectAtIndex:indexPath.row] valueForKey:@"content"];
//        editVC.delegate = self;
        
    }
    if (indexPath.section == 2) {
        
        if (indexPath.row == 1) {
            CityViewController *cityVC = [[CityViewController alloc] init];
            [self.navigationController pushViewController:cityVC animated:YES];
            NSString *str = [_sectionArr objectAtIndex:indexPath.section];
            NSArray *arr = [_dataDic objectForKey:str];
            cityVC.titleName = [[arr objectAtIndex:indexPath.row] valueForKey:@"name"];
//            cityVC.promptInfo = [[arr objectAtIndex:indexPath.row] valueForKey:@"content"];
            
        }
        
        if (indexPath.row == 2) {
            DownListViewController *downVC = [[DownListViewController alloc] init];
            [self.navigationController pushViewController:downVC animated:YES];
            NSString *str = [_sectionArr objectAtIndex:indexPath.section];
            NSArray *arr = [_dataDic objectForKey:str];
            downVC.titleName = [[arr objectAtIndex:indexPath.row] valueForKey:@"name"];
            downVC.promptInfo = [[arr objectAtIndex:indexPath.row] valueForKey:@"content"];

        }
        
        if (indexPath.row == 3) {
            PersonalInfomationViewController *perVC = [[PersonalInfomationViewController alloc] init];
            [self.navigationController pushViewController:perVC animated:YES];
            NSString *str = [_sectionArr objectAtIndex:indexPath.section];
            NSArray *arr = [_dataDic objectForKey:str];
            perVC.titleName = [[arr objectAtIndex:indexPath.row] valueForKey:@"name"];
//            perVC.promptInfo = [[arr objectAtIndex:indexPath.row] valueForKey:@"content"];
            
        }
        
        
        if (indexPath.row == 4) {
            FieldViewController *fieldVC = [[FieldViewController alloc] init];
            [self.navigationController pushViewController:fieldVC animated:YES];
            NSString *str = [_sectionArr objectAtIndex:indexPath.section];
            NSArray *arr = [_dataDic objectForKey:str];
            fieldVC.titleName = [[arr objectAtIndex:indexPath.row] valueForKey:@"name"];
            //            perVC.promptInfo = [[arr objectAtIndex:indexPath.row] valueForKey:@"content"];
            
        }

    }
}


- (void)bringStr:(NSString *)str{
    self.text = str;
    NSLog(@"%@",self.text);
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
